﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessReading
    {
        #region Create
        public static void createReading(Reading r)
        {
            try
            {
                dataReading.createReading(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating reading");
            }
        }

        public static void createReading(Land l, float pH, float k, float p, float na)
        {
            try
            {
                Reading r = new Reading();
                r.LandID = l.LandID;
                r.pH = pH;
                r.Phosphorus = k;
                r.Potassium = p;
                r.Nitrogen = na;

                dataReading.createReading(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating reading");
            }
        }
        #endregion

        #region Get
        public static Reading getReading(Guid guid)
        {
            try
            {
                return dataReading.getReading(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting reading");
            }
        }

        public static List<Reading> getReadingsByLand(Land l)
        {
            try
            {
                Expression<Func<Reading, bool>> whereClause = (Reading r) => (r.LandID == l.LandID);

                return dataReading.getReadings(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting readings by land");
            }
        }
        #endregion

        #region Set
        public static Boolean changeReading(Reading r)
        {
            try
            {
                return dataReading.changeReading(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing reading");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteReading(Reading r)
        {
            try
            {
                return dataReading.deleteReading(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deleting reading");
            }
        }
        #endregion
    }
}
